﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cole.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Cole.Repository
{
    public class MatriculaRepository : MasterRepository, IMatriculaRepository
    {
        public List<MatriculaDTO> GetAllFromMatriculasByCriterio(string criterio)
        {
            var param = new object[] { criterio, 0 };

            var query = _database.ExecuteSprocAccessor<MatriculaDTO>
                ("GetFromPedidoByClienteAndFormaPagoIdDTO", param);

            return query.ToList();
        }

        public void AddMatricula(MatriculaDTO matricula)
        {
            var command = _database.GetStoredProcCommand("RegistrarMatricula");

            if (matricula.Id != 0)
            {
                _database.AddInParameter(command, "id", DbType.Int32, matricula.Id);
            }
            else
            {
                _database.AddOutParameter(command, "id", DbType.Int32, 9);
            }

            _database.AddInParameter(command, "IdAlumno", DbType.Int32, matricula.IdAlumno);
            _database.AddInParameter(command, "fecha", DbType.DateTime, matricula.Fecha);            
           

            _database.ExecuteNonQuery(command);

            if (matricula.Id == 0)
            {
                matricula.Id = Int32.Parse(_database.GetParameterValue(command, "id").ToString());
            }

            foreach (var item in matricula.Items)
            {
                var commandDetalle = _database.GetStoredProcCommand("RegistrarDetalleMatricula");

                if (item.Item == 0)
                {
                    _database.AddOutParameter(commandDetalle, "item", DbType.Int32, 9);
                }
                else
                {
                    _database.AddInParameter(commandDetalle, "item", DbType.Int32, item.Item);
                }

                _database.AddInParameter(commandDetalle, "IdMatricula", DbType.Int32, matricula.Id);
                _database.AddInParameter(commandDetalle, "IdCurso", DbType.Int32, item.IdCurso);
                _database.AddInParameter(commandDetalle, "cantidad", DbType.Decimal, item.Cantidad);                

                _database.ExecuteNonQuery(commandDetalle);

                if (item.Item == 0)
                {
                    item.Item = Int32.Parse(_database.GetParameterValue(commandDetalle, "item").ToString());
                }
            }
        }

        public void RemoveMatricula(int id)
        {
            throw new NotImplementedException();
        }

        public MatriculaDTO GetMatriculaById(int id)
        {
            
            var query = _database
               .ExecuteSprocAccessor<MatriculaDTO>("GetFromPedidoByIdDTO", id);

            var items = _database
                .ExecuteSprocAccessor<DetalleMatriculaDTO>("GetFromDetallePedidoByPedidoIdDTO", id);

            var pedido = query.Single();

            pedido.Items = items.ToList();

            return pedido;
        }

        public void QuitarDetalle(int item)
        {
            var command = _database.GetStoredProcCommand("RemoveDetallePedido");

            _database.AddInParameter(command, "item", DbType.Int32, item);

            _database.ExecuteNonQuery(command);
        }
    }
}
